<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" >
        <el-form-item>
          <el-input v-model="keyword" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="keywordquery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="department" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="sn" label="部门编号" width="120" sortable>
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="100" sortable>
      </el-table-column>
      <el-table-column prop="dirPath" label="部门路径" width="120"  sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" width="100" sortable>
        <template slot-scope="scope">
          <span style="color: green" v-if="scope.row.state==1">正常</span>
          <span style="color: red" v-else-if="scope.row.state==0">停用</span>
          <span style="color: darkslategray" v-else>未知</span>
        </template>
      </el-table-column>
      <el-table-column prop="employee.username" label="部门经理" width="120" sortable>
      </el-table-column>
      <el-table-column prop="department.name" label="上级部门" width="120" sortable>
      </el-table-column>
      <el-table-column label="操作" >
        <template scope="scope">
          <el-button type="primary" icon="el-icon-save" circle @click="handlesave(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" v-perm="'department:delete'"  icon="el-icon-delete" circle @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" v-perm="'department:patchDel'"  @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
          layout="total,sizes, prev, pager, next,jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="sizes"
          :page-size="pageSize"
          :total="totals"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--添加修改-->
    <!--编辑界面-->
    <!--
      title="添加或修改" - 标题
      :visible.sync="saveFormVisible" - 控制当前对话框是否显示 - 默认为false不显示
      :close-on-click-modal="false" - 点击对话框以外的区域不关闭
    -->
    <el-dialog title="添加或修改" :visible.sync="saveFormVisible" :loading="saveLoading" :close-on-click-modal="false">
      <!--
        :model="saveForm"  - 表单绑定模型数据对象saveForm
        label-width="80px"  - 标签的宽度
        :rules="saveFormRules" - 验证规则 - 表单项校验
        ref="saveForm" - 为当前对话框取名saveForm - 做表单校验

        label="姓名" - 标签名字
        prop="name" - 要做校验的时候必须要，不做校验就没有必要
        v-model="saveForm.name" - 双向绑定
        auto-complete="off" - 自动完成 - 关闭
      -->
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="编号" prop="sn">
          <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="1">启用</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="部门经理">
          <el-select v-model="saveForm.manager_id" placeholder="请选择">
            <el-option
                v-for="item in employees"
                :label="item.username"
                :value="item.id">
              <span style="float: left">{{ item.username }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.phone }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上级部门">
          <el-cascader v-model="saveForm.parent_id"
                    :options="deptTree"
                    :props="{
            	      checkStrictly: true,
                    label: 'name', // 级联框显示那个属性的值
                    value: 'id'    // 回显时需要,回传值的时候也需要
                    }" clearable></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" >提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
export default {

  data() {
    return {
      //1.分页相关参数
      department: [],     //绑定到表格 - 显示列表数据 - 请求完成之后
      totals: 0,          //总数量 - 计算分页栏 -  请求完成之
      currentPage: 1,     //当前页 - 分页栏默认当前页显示
      pageSize: 5,        //每页显示的条数  - 计算分页栏
      sizes:[5, 10, 20, 50],
      listLoading: false, //表格上的加载框

      //2.高级查询相关数据
      keyword: "",

      //3.批量删除相关参数
      sels: [],

      //4.添加或修改的相关数据
      saveFormVisible: false, //控制对话框是否显示 - 默认不显示
      saveLoading: false,     //保存的加载框 - 默认不显示
      saveFormRules: {        //表单项验证
        sn: [
          {required: true, message: '请输入编号', trigger: 'blur'}
        ],
        name: [
          {required: true, message: '请输入名称', trigger: 'blur'}
        ]
      },
      //5.添加和修改中对话框的数据
      saveForm: {
        id: null,
        sn: '',
        name: '',
        state: 1,
        manager_id: null,
        parent_id: null
      },
      //6.回显部门经理名称选择器相关数据
      employees: [],
      //7.联级
      deptTree:null
    }
  },
  methods: {
    //1.每页条数
    handleSizeChange(val) {
      this.pageSize=val;
      this.getDepartment();
    },
    //2.当前页改变
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getDepartment();
    },
    //3.高级查询
    keywordquery(){
      this.currentPage=1;
      this.getDepartment();
    },
    //1.获取用户列表
    getDepartment() {
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.keyword
      };
      this.listLoading = true;

      this.$http.post("/department",para).then(res=>{

        //给totals赋值 - 计算分页栏
        this.totals = res.data.totals;
        //赋值分页数据 - 显示当前页的分页数据
        this.department = res.data.data;
        //隐藏加载框/忙等框
        this.listLoading = false;
      }).catch(res=> {
        this.$message.error('系统繁忙，请稍后重试!!!');
      })
    },
    //2.删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/department/"+row.id).then((res)=>{
          this.listLoading = false;
          if (res.data.success){
            this.$message.success("删除成功");
            if(((this.currentPage*this.pageSize)-this.totals)>=(this.pageSize-1)){
              this.currentPage=this.currentPage-1;
            }
            this.getDepartment();
          }else {
            this.$message.error("系统繁忙，请稍后再试！！！");
          }
        }).catch((res) => {
          this.$message.error("系统繁忙，请稍后再试！！！");
        });
      }).catch(()=>{

      })
    },
    //3.批量删除
    selsChange: function (sels) {
      this.sels = sels;
    },
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/department", ids).then((res) => {
          this.listLoading = false;
          if (res.data.success) {
            this.$message.success("删除成功");
            this.currentPage =1;
            this.getDepartment();
          } else {
            this.$message.error("系统繁忙，请稍后再试！！！");
          }
        }).catch((res) => {
          this.$message.error("系统繁忙，请稍后再试！！！");
        });
      }).catch(() => {

      });
    },
    //4.显示编辑界面
    handlesave:function (index, row) {
      this.saveFormVisible = true;
      this.saveForm = Object.assign({}, row);
      this.getemployee();
      this.getdeptTree();
    },
    //5.显示新增界面
    handleAdd: function () {
      this.saveFormVisible = true;
      this.saveForm  = {
        id: null,
        sn: '',
        name: '',
        state: 1,
        manager_id: null,
        parent_id: null
      };
      this.getemployee();
      this.getdeptTree();
    },
    //6.添加修改
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.saveLoading = true;
            let para = Object.assign({}, this.saveForm);
            let arr=para.parent_id;
            if (Array.isArray(arr)){
              if (arr.length==0){
                para.parent_id=null;
              }else {
                para.parent_id=arr[arr.length-1];
              }
            }
            this.$http.put("/department", para).then((res) => {
              this.saveLoading = false;
              this.saveFormVisible = false;
              if (res.data.success) {
                this.$message.success("操作成功");
                this.getDepartment();
              }else {
                this.$message.error("系统繁忙，请稍后再试！！！");
              }
            }).catch((res)=>{
              this.$message.error("系统繁忙，请稍后再试！！！");
            });
          });
        }
      }).catch(()=>{

      });
    },
    //7.获取  员工数据t_employee
    getemployee(){
      this.$http.get("/employee").then((res)=>{
        this.employees=res.data;
      })
    },
    //8.获取deptTree数据
    getdeptTree(){
      this.$http.get("/department/getTree").then((res)=>{
        this.deptTree=res.data;
      })
    }
  },
  mounted() {
    this.getDepartment();
  }
}
</script>

<style scoped>

</style>